package com.lanou3g.code0413.stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by dllo on 18/4/13.
 */
public class CopyTest {
    public static void main(String[] args) {
        String srcPath = "/Users/dllo/Desktop/aaa.png";  //我们要复制的文件 (绝对路径 / 开始,结尾木有空格)
        String desPath = "/Users/dllo/Desktop/bb.jpg";  //要复制到的文件(新文件)

        File srcFile = new File(srcPath);
        File desFile = new File(desPath);

        FileInputStream fis = null;
        FileOutputStream fos = null;

        //复制功能
        try {
            //创建目标文件
            desFile.createNewFile();

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(desFile);

            byte[] buf = new byte[1024]; //准备一个瓢
            int b = 0;
            while ((b = fis.read(buf)) != -1) {
                // 写到输出流中
                fos.write(buf, 0, b);
            }

        } catch (Exception e) {  // 变成Exception
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
